<template>
  <div class="login login_style">
    <div class="bg" v-if="false">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <h3 class="title"></h3>
        <div style="margin-bottom: 20px" align="right">
          <el-link @click="dumpUrl('register')" style="margin-bottom: 5px"
            >没有账号立即免费注册</el-link
          >
          <i class="el-icon-caret-right"></i>
        </div>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 60%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="validCode"
              class="el-input__icon input-icon"
            />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-checkbox
              v-model="loginForm.rememberMe"
              style="margin: 0px 0px 25px 0px"
              >记住密码</el-checkbox
            >
          </el-col>
          <el-col :span="12">
            <div align="right">
              <el-link @click="dumpUrl('retrieve')" style="margin-bottom: 5px"
                >忘记密码？</el-link
              >
            </div>
          </el-col>
        </el-row>
        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="
              width: 100%;
              background: linear-gradient(54deg, #0057a1, #4087c8);
              height: 38px;
            "
            @click.native.prevent="handleLogin"
          >
            <span style="font-weight: bold;" v-if="!loading">立 即 登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="login_logo">
      <div class="login_logo_img"></div>
      <div class="login_logo_txt_box">
        <span class="login_logo_txt">文嘟嘟</span>
      </div>
    </div>
    <div class="login_content">
    <div class="login_box">
      <div class="login_box_content">
          <div class="login_box_top">
            <h3>登录</h3>
            <el-link style="display:none" class="to_register" :underline="false" @click="dumpUrl('register')">注册>></el-link>
          </div>
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            label-width="0"
          >
            <el-form-item prop="username" class="form-item">
              <div class="item_form">
                <h4 class="item_form_label"><span>账</span> <span>户</span></h4>
                <el-input type="text" class="login_input" auto-complete="off" v-model.trim="loginForm.username"  @keyup.enter.native="handleLogin" placeholder="请输入登录账号" />
              </div>
            </el-form-item>
            <el-form-item prop="password" class="form-item">
              <div class="item_form">
                <h4 class="item_form_label"><span>密</span><span>码</span></h4>
                <el-input type="password" class="login_input" auto-complete="off" v-model.trim="loginForm.password"  @keyup.enter.native="handleLogin" placeholder="请输入登录密码" />
              </div>
            </el-form-item>
            <el-form-item prop="code" class="form-item">
              <div class="item_form">
                <h4 class="item_form_label"><span>验</span><span>证</span><span>码</span></h4>
                <el-input type="text" class="login_input" auto-complete="off" v-model.trim="loginForm.code"  @keyup.enter.native="handleLogin" placeholder="请输入验证码" />
                <div class="login-code">
                  <img :src="codeUrl" @click="getCode" class="login-code-img" />
                </div>
              </div>
            </el-form-item>
            <el-row>
              <el-col :span="12">
                <el-checkbox
                  v-model="loginForm.rememberMe"
                  style="margin: 0px 0px 25px 0px"
                  ><span style="font-size: 16px;color: #999">记住登录密码</span></el-checkbox
                >
              </el-col>
              <el-col :span="12">
                <div align="right" style="display:none">
                  <el-link @click="dumpUrl('retrieve')" type="primary" style="font-size: 16px;margin-bottom: 5px;">忘记密码？</el-link>
                </div>
              </el-col>
            </el-row>
            <el-form-item style="width: 100%">
              <el-button
                :loading="loading"
                type="primary"
                style="
                  width: 100%;
                  height: 46px;
                  font-size: 18px;
                  margin-top: 35px;
                "
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">立 即 登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
            </el-form-item>
        </el-form>
      </div>
    </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <Footer />
    </div>
  </div>
</template>

<script>
import { getCodeImg, getInfo } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: "blur",
            message: "用户名不能为空",
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            message: "密码不能为空",
          },
        ],
        code: [
          {
            required: true,
            trigger: "change",
            message: "验证码不能为空",
          },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;

      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, {
              expires: 30,
            });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              getInfo().then((response) => {
                var arr = response.roles;
                let index = 0;
                arr.forEach((element) => {
                  if ("store" === element) {
                    index++;
                  }
                });
                this.$router
                    .push({
                      path: "index",
                    })
                    .catch(() => {});
              });
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
        }
      });
    },
    dumpUrl(ul) {
      let url = window.location.href;
      let arrS = url.split("login");
      location.href = arrS[0] + ul;
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  width: 100%;
  min-height: 100%;
  //background: url("../assets/images/login_bg.png") no-repeat center;
  background-size: cover;
  position: relative;
  .login_logo {
    position: absolute;
    top: 2vh;
    left: 3vw;
    display: flex;
    align-items: center;
    .login_logo_img {
      width: 65px;
      height: 65px;
      background: url("../assets/images/login_logo.png") no-repeat center;
      margin-right: 10px;
    }
    .login_logo_txt_box {
      height: 24px;
      width: 120px;
      position: relative;
      .login_logo_txt {
        position: absolute;
        top: 0;
        left: 0;
        background: linear-gradient(54deg, #0057A1, #4087C8);
        -webkit-background-clip: text;
        color: transparent;
        -webkit-text-fill-color: transparent;
        text-fill-color: transparent;
        font-size: 24px;
        line-height: 1;
        font-weight: bold;
      }
    }
  }
}
.login_content {
  height: 88vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login_box {
  width: 480px;
  height: auto;
  background-color: #FFF;
  box-shadow: 0px 0px 40px 0px rgba(27, 46, 107, 0.05);
  border-radius: 20px;
  padding: 74px 0 36px;
  .login_box_content {
    width: 334px;
    height: auto;
    margin: 0 auto;
    .login_box_top {
      display: flex;
      align-items: flex-end;
      justify-content: space-between;
      margin-bottom: 30px;
      h3 {
        color: #5DA9EF;
        font-weight: bold;
        font-size: 30px;
        line-height: 1;
      }
      .to_register {
        line-height: 1;
        font-size: 19px;
        color: #999;
      }
    }
    .form-item {
      border-bottom: 1px solid #eee;
    }
    .item_form {
      display: flex;
      align-items: center;
      .item_form_label {
        display: flex;
        justify-content: space-between;
        width: 60px;
        color: #666;
        font-size: 18px;
        line-height: 1;
        font-weight: normal;
        line-height: 50px;
      }
      .login_input {
        padding:0;
        border:none;
        outline: none;
        flex: 1;
        overflow: hidden;
        height: 50px;
        font-size: 16px;
        color: #333;
        padding: 0 16px 0 40px;
      }
    }
  }
}



.bg {
  width: 1168px;
  height: 635px;
  //background-image: url("../assets/images/Signbg.png");
  position: relative;
  margin-bottom: 80px;
}


.title {
  width: 286px;
  height: 56px;
  //background-image: url("../assets/images/youxiao.png");
  margin: 0px auto;
  text-align: center;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  position: absolute;
  top: 120px;
  right: 100px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  height: 38px;
  position: relative;
  top: -2px;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  letter-spacing: 1px;
  margin-top: 10px;
}

.login-code-img {
  height: 38px;
}
</style>
